package com.spikything.video {
	
	import com.spikything.gfx.RoundFrame;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import com.spikything.video.events.VideoEvent;
	import com.spikything.gfx.Scale9Bitmap;

	public class VideoScrubber extends Sprite {
		
		private const 	SCRUBBER_COLOUR	:Number	 	= 0xFFFFFF;// 0xA15813;// 0xE0F0FC;
		private const 	SCRUBBER_BG		:Number	 	= 0x000000;// 0xA15813;//0x66380C;// 0xE0F0FC;
		private const 	BG_ALPHA 		:Number 	= .3;
		private const 	SCRUBBER_ALPHA 	:Number 	= 1;
		private const 	SCRUBBER_HEIGHT :Number 	= 9;// 9;
		private const 	SCRUBBER_ROUND 	:Number 	= 0;// 8;
		private const 	PADDING 		:Number 	= 0;// 2;
		private var 	mouseIsDown 	:Boolean;
		private var 	scrubber 		:Sprite;
		private var 	progress 		:Sprite;
		private var 	maskClip 		:Sprite;
		private var 	beacon 			:EventDispatcher;
		private var 	_position 		:Number;
		
		public var disabled:Boolean = false;
		
		//[Embed(source='../../../../assets/gfx/player_scrubber_bg.png')]
		//private var 	bgClass  		:Class;
		//private var 	background 		:Scale9Bitmap;
		private var background:RoundFrame;
		
		public function VideoScrubber (beacon:EventDispatcher, x:Number=0, y:Number=0, width:Number=100) {
			
			this.beacon = beacon;
			this.x = x;
			this.y = y;
			
			//background = new Scale9Bitmap((new bgClass() as Bitmap).bitmapData, new Rectangle(4, 4, 323, 4));
			background = new RoundFrame(width, SCRUBBER_HEIGHT, SCRUBBER_BG, BG_ALPHA);
			addChild(background);
			
			maskClip = Sprite(addChild(new Sprite() as DisplayObject));
			maskClip.x = maskClip.y = PADDING;
			
			scrubber = new Sprite();
			scrubber.graphics.beginFill(SCRUBBER_COLOUR, SCRUBBER_ALPHA);
			scrubber.graphics.drawRect(0, 0, width, SCRUBBER_HEIGHT+PADDING);
			addChild(scrubber);
			scrubber.mask = maskClip;
			
			scrubber.width = _position = 0;
			
			buttonMode = true;
			mouseChildren = false;
			beacon.addEventListener(VideoEvent.INFO_POSITION, update);
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			
			this.width = width;
		}
		
		//public override function get width ():Number { return background.width };
		public override function set width (value:Number):void {
			var ve:VideoEvent = new VideoEvent(VideoEvent.INFO_POSITION);
			ve.position = _position;
			//background.width = value;
			maskClip.graphics.clear();
			maskClip.graphics.beginFill(0);
			maskClip.graphics.drawRoundRect(0, 0, width-PADDING*2, SCRUBBER_HEIGHT, SCRUBBER_ROUND, SCRUBBER_ROUND);
			update(ve);
		}
		
		public function update (ve:VideoEvent):void {
			if (disabled) return;
			_position = ve.position;
			scrubber.width = ve.position * width;
		}
		
		private function mouseMove (e:MouseEvent):void {
			if (!mouseIsDown) return;
			var ve:VideoEvent = new VideoEvent(VideoEvent.INFO_POSITION);
			ve.position = getPos(mouseX);
			update(ve);
		}

		private function mouseDown (e:MouseEvent):void {
			if (disabled) return;
			mouseIsDown = true;
			beacon.removeEventListener(VideoEvent.INFO_POSITION, update);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
			mouseMove(e);
		}

		private function mouseUp (e:MouseEvent):void {
			mouseIsDown = false;
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
			var ve:VideoEvent = new VideoEvent(VideoEvent.INFO_POSITION);
			var vs:VideoEvent = new VideoEvent(VideoEvent.DO_SEEKTO);
			ve.position = vs.position = getPos(mouseX);
			beacon.addEventListener(VideoEvent.INFO_POSITION, update);
			//beacon.addEventListener(VideoEvent.INFO_PLAYING, onPlay);
			beacon.dispatchEvent(vs);
			update(ve);
		}
		
		private function getPos (where:Number):Number {
			return Math.max(Math.min(1, where/width), 0);
		}
		
		//private function onPlay (e:Event):void { beacon.addEventListener(VideoEvent.INFO_POSITION, update) };
		
	}
	
}